package com.example.smartcity.ui.dashboard;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.HospitalListViewAdapter;
import com.example.smartcity.ui.Bean.HospitalListViewBean;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class OrderHospital extends AppCompatActivity {
    private android.widget.SearchView SearchView;
    private ListView hospitalListView;
    private String TAG = "TAG";
    private List<HospitalListViewBean.RowsBean> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_hospital);
        SearchView = findViewById(R.id.SearchView);
        hospitalListView = findViewById(R.id.hospitalListView);
        initView();
        SearchFunction();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/registration/list?pageNum=1&pageSize=10";
        OkHttpUtils.getCall(newUrl).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                String Json = response.body().string();
                HospitalListViewBean bean =  gson.fromJson(Json,HospitalListViewBean.class);
                if (bean.getCode()==200){
                    list = bean.getRows();
                    hospitalListView.post(new Runnable() {
                        @Override
                        public void run() {
                            hospitalListView.setAdapter(new HospitalListViewAdapter(getBaseContext(),R.layout.hospitalitemlayout,list));
                        }
                    });
                }else{
                    Looper.prepare();
                    Toast.makeText(OrderHospital.this, "加载失败", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            }
        });

        hospitalListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int hospitalId = list.get(position).getId();
                Intent intent = new Intent(OrderHospital.this,HospitalMessage.class);
                intent.putExtra("hospitalId",hospitalId);
                startActivity(intent);
            }
        });
    }


    public void SearchFunction(){
       // hospitalListView.setTextFilterEnabled(true);

        SearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                List<HospitalListViewBean.RowsBean> list1 = new ArrayList<>();
                if (!TextUtils.isEmpty(newText)){
                    for (int i = 0; i < list.size(); i++) {
                        if(list.get(i).getHospitalName().contains(newText)){
                            list1.add(list.get(i));
                        }
                    }
                    hospitalListView.setAdapter(new HospitalListViewAdapter(getBaseContext(),R.layout.hospitalitemlayout,list1));
                }else{
                    hospitalListView.clearTextFilter();
                }
                return false;
            }
        });
    }
}